/**   
 * @Title: HlsPlayController.java 
 * @Package android.framework.media.octo 
 * @Description: TODO
 * @author jtf@163.com   
 * @date 2013-7-19 下午4:56:37 
 * @version V1.0   
 */
package android.framework.media.hls;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.framework.media.BaseMediaController;
import android.framework.media.MediaController;
import android.framework.media.PlayController;
import android.framework.media.VideoView;
import android.framework.media.bean.ShackVideoInfo;
import android.framework.util.L;
import android.framework.util.UIs;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.SeekBar.OnSeekBarChangeListener;

import android.framework.R;

/**
 * @ClassName: HlsPlayController
 * @Description: TODO
 * @author jtf19@163.com
 * @date 2013-7-19 下午4:56:37
 * 
 */
public class HlsPlayController implements PlayController {
	protected Activity activity;

	protected int errorCount;
	protected Bundle mBundle;
	protected MediaController mediaController;
	protected boolean more;
	protected int position;
	protected View progressView;
	protected View loadingProgress;
	protected View loadingDialog;
	protected VideoView videoView;
	protected String title;
	protected String url;

	public HlsPlayController(Activity activity) {
		super();
		this.activity = activity;
	}

	protected void createMediaController() {
		this.mediaController = new MediaController(this.activity);
		this.mediaController.setPlayController(this);
		this.mediaController.setMbundle(this.mBundle);
		this.mediaController
				.setOnSeekBarChangeListener(mOnSeekBarChangeListener);
	}

	protected void finish() {
		if (videoView != null) {
			videoView.stopPlayback();
		}
		this.activity.finish();
	
	}

	protected Context getContext() {
		return this.activity;
	}

	public String getFrom() {
		return "8";
	}

	public BaseMediaController getMediaController() {
		return this.mediaController;
	}

	public Bundle getPlayBundle() {
		return this.mBundle;
	}

	public ShackVideoInfo getVideoInfo() {
		return null;
	}

	public String getVideoTitle() {
		return this.title;
	}

	public VideoView getVideoView() {
		return this.videoView;
	}

	public void handlerAdClick() {
	}

	protected void initView() {
		this.loadingDialog = this.activity
				.findViewById(R.id.play_loading_dialog);
		this.progressView = this.activity.findViewById(R.id.play_progressview);
		this.loadingProgress = this.activity.findViewById(R.id.progress);
		RelativeLayout localRelativeLayout = (RelativeLayout) this.activity
				.findViewById(R.id.videoview_container);
		this.videoView = new VideoView(getContext());
		this.videoView.setLayoutParams(new RelativeLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		((RelativeLayout.LayoutParams) this.videoView.getLayoutParams())
				.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
		localRelativeLayout.addView(this.videoView);
	}

	public boolean isLive() {
		return false;
	}

	public boolean isLoadingShown() {
		if ((this.progressView != null)
				&& (this.progressView.getVisibility() == View.VISIBLE))
			return true;
		return false;
	}

	public boolean isPlayingAd() {
		return false;
	}

	public void onActivityResult(int paramInt1, int paramInt2,
			Intent paramIntent) {
	}

	public void onCompletion(MediaPlayer paramMediaPlayer) {
		finish();
	}

	public void onCreate(Bundle paramBundle) {
		this.mBundle = paramBundle;
		this.title = paramBundle.getString("title");
		this.url = paramBundle.getString("url");
		this.position = paramBundle.getInt("timeLengthInTs", 0);
		initView();
	}

	public void onDestroy() {
		if (videoView != null) {
			videoView.stopPlayback();
		}
	}

	public boolean onError(MediaPlayer paramMediaPlayer, int paramInt1,
			int paramInt2) {
		//UIs.notifyShortNormal(activity, "error count  " + errorCount);
		this.position = this.videoView.getCurrentPosition();
		HlsPlayController.this.errorCount += 1;
		L.d("Mediaplayer", "error count  " + errorCount + " paramInt1 " + paramInt1 + " paramInt2 " + paramInt2 + " position "  + position);
		Toast.makeText(activity, "error count  " + errorCount + " paramInt1 " + paramInt1 + " paramInt2 " + paramInt2  + " position "  + position, 1).show();
		
		if (HlsPlayController.this.errorCount <= 3) {
			HlsPlayController.this.loadingProgress.setVisibility(View.VISIBLE);
			play(HlsPlayController.this.url, HlsPlayController.this.title);
		} else {
			finish();
			// Toast.makeText(activity, "播放重连3次错误", Toast.LENGTH_SHORT).show();
		}
		return false;
	}

	public void onFinish() {
		if (videoView != null) {
			videoView.stopPlayback();
		}
		this.activity.finish();
	}

	public void onPause() {
		this.more = true;
		this.position = this.videoView.getCurrentPosition();
		this.videoView.pause();
	}

	protected void applyWindowFullScreen() {
		this.activity.getWindow().setFlags(
				WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}

	public void onPrepared(MediaPlayer paramMediaPlayer) {
		applyWindowFullScreen();
		this.progressView.setVisibility(View.GONE);
		this.loadingProgress.setVisibility(View.GONE);
	}

	public void onResume() {
		play(this.url, this.title);
		if (!this.more)
			return;
		this.loadingProgress.setVisibility(View.VISIBLE);
	}

	public void onStopTrackingTouch() {
	}

	public void onVideoPause() {
	}

	public void onVideoStart() {
	}

	protected void play(String url, String videoTitle) {
		try {
			Uri localUri = Uri.parse(url);
			this.videoView.setVideoURI(localUri);
			this.videoView.setVideoTitle(videoTitle);
			createMediaController();
			this.videoView.setMediaController(getMediaController());
			this.videoView.setOnErrorListener(mErrorListener);
			this.videoView.setOnCompletionListener(mCompletionListener);
			this.videoView.setOnPreparedListener(mPreparedListener);
			this.videoView.setOnSeekCompleteListener(mOnSeekCompleteListener);
			this.videoView.setOnInfoListener(mOnInfoListener);
			this.videoView.requestFocus();
			this.videoView.start();
			if (this.position <= 0)
				return;
			this.videoView.seekTo(this.position);
		} catch (Exception e) {
			e.printStackTrace();
			L.i(e.getMessage());
			// TODO: handle exception
		}
	}

	MediaPlayer.OnInfoListener mOnInfoListener = new MediaPlayer.OnInfoListener() {

		@Override
		public boolean onInfo(MediaPlayer mp, int what, int extra) {
			// TODO Auto-generated method stub
			Log.d("Mediaplayer", "what  " + what + "  extra  " + extra);

			return false;
		}
	};

	MediaPlayer.OnSeekCompleteListener mOnSeekCompleteListener = new MediaPlayer.OnSeekCompleteListener() {

		@Override
		public void onSeekComplete(MediaPlayer mp) {
			L.d("MediaPlayer", "seekcomplete");
			loadingDialog.setVisibility(View.GONE);
		}
	};

	protected OnSeekBarChangeListener mOnSeekBarChangeListener = new OnSeekBarChangeListener() {

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {

		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			L.d("MediaPlayer", "onStartTrackingTouch");
			loadingDialog.setVisibility(View.VISIBLE);
		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			// TODO Auto-generated method stub

		}
	};

	private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {

		@Override
		public void onCompletion(MediaPlayer mp) {
			// TODO Auto-generated method stub
			finish();
		}
	};

	private MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {

		@Override
		public void onPrepared(MediaPlayer mp) {
			applyWindowFullScreen();
			HlsPlayController.this.progressView.setVisibility(View.GONE);
			HlsPlayController.this.loadingProgress.setVisibility(View.GONE);

		}
	};

	private MediaPlayer.OnErrorListener mErrorListener = new MediaPlayer.OnErrorListener() {

		@Override
		public boolean onError(MediaPlayer mp, int what, int extra) {
			HlsPlayController.this.onError(mp, what, extra);
			return false;
		}
	};

	@Override
	public void updateVideoPosition() {
		// TODO Auto-generated method stub

	}

	public boolean onTouchEvent(MotionEvent paramMotionEvent) {
		return false;
	}
}
